import {
  setToken,
  getToken,
  removeToken
} from '../../utils/token'
//登录和注册
import {
  reqGetCode,
  reqUserRegister,
  reqLogin,
  reqUserInfo,
  reqLogout
} from "../../api"
const state = {
  code: '',
  token: getToken(),
  userInfo: {}
}
const mutations = {
  GETCODE(state, code) {
    state.code = code
  },
  USERLOGIN(state, token) {
    state.token = token
  },
  USERINFO(state, userInfo) {
    state.userInfo = userInfo
  },
  //清除本地数据
  CLEAR(state) {
    state.token = ''
    state.userInfo = {}
    removeToken()
  }
}
const actions = {
  //获取验证码
  async getCode({
    commit
  }, phone) {
    let result = await reqGetCode(phone)
    if (result.code == 200) {
      commit('GETCODE', result.data)
      return 'ok'
    } else {
      return Promise.reject(new error('faile'))
    }

  },
  //注册
  async userRegister({
    commit
  }, user) {
    let result = await reqUserRegister(user)
    if (result.code == 200) {
      return 'ok'
    } else {
      return Promise.reject(new Error('faile'))
    }
  },
  //登录
  async userLogin({
    commit
  }, data) {
    let result = await reqLogin(data)
    if (result.code == 200) {
      commit('USERLOGIN', result.data.token)
      // localStorage.setItem('TOKEN',result.data.token)
      setToken(result.data.token)
      return 'ok'

    } else {
      return Promise.reject(new Error('faile'))
    }
  },
  //获取用户信息
  async getUserInfo({
    commit
  }) {
    let result = await reqUserInfo()
    if (result.code == 200) {
      commit('USERINFO', result.data)
      return 'ok'
    } else {
      return Promise.reject(new Error('faile'))
    }

  },
  //退出登录
  async userLogout({
    commit
  }) {
    let result = await reqLogout()
    if (result.code == 200) {
      commit('CLEAR')
      return 'ok'
    } else {
      return Promise.reject(new Error('faile'))
    }
  }
}
const getters = {}
export default {
  state,
  mutations,
  actions,
  getters
}